package com.greyu.ysj.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.greyu.ysj.entity.CbtChangeDraw;
import com.greyu.ysj.entity.CbtChangeDrawExample;
import com.greyu.ysj.entity.CbtChangeExample;
import com.greyu.ysj.entity.CbtRecharge;
import com.greyu.ysj.entity.CbtRechargeExample;
import com.greyu.ysj.mapper.CbtChangeDrawMapper;
import com.greyu.ysj.service.CbtChangeDrawService;

@Service
public class CbtChangeDrawServiceImpl implements CbtChangeDrawService{
@Autowired
private CbtChangeDrawMapper cbtChangeDrawMapper;
	@Override
	public List<CbtChangeDraw> getChangeRecord(Integer userId,Integer coinId, Integer page, Integer rows) {
		// TODO Auto-generated method stub
		if (null != page && null != rows) {
            PageHelper.startPage(page, rows);
        }
		CbtChangeDrawExample cbtChangeDrawExample = new CbtChangeDrawExample();
		CbtChangeDrawExample.Criteria criteria = cbtChangeDrawExample.createCriteria();
		criteria.andUserIdEqualTo(userId);
		criteria.andCoinIdEqualTo(coinId);
		cbtChangeDrawExample.setOrderByClause("`create_date` DESC");
		return cbtChangeDrawMapper.selectByExample(cbtChangeDrawExample);
	}

	@Override
	public int totalPages(Integer userId, Integer rows) {
		// TODO Auto-generated method stub
		CbtChangeDrawExample cbtChangeDrawExample = new CbtChangeDrawExample();
		CbtChangeDrawExample.Criteria criteria =cbtChangeDrawExample.createCriteria();
		criteria.andUserIdEqualTo(userId);
		long total=this.cbtChangeDrawMapper.countByExample(cbtChangeDrawExample);
		int totalPages;
		if(total%rows == 0) {
			totalPages=(int) (total/rows);
		}else {
			totalPages=(int) (total/rows)+1;
		}
		System.out.println("totalPages_change=>"+totalPages);
		return totalPages;
	}

	@Override
	public List<CbtChangeDraw> changeMoneyList(Integer page, Integer rows, Integer userId, Integer coinId,
			String startTime, String endTime) throws ParseException {
		// TODO Auto-generated method stub
		
		if (null != page && null != rows) {
            PageHelper.startPage(page, rows);
        }
		
		CbtChangeDrawExample cbtChangeDrawExample = new CbtChangeDrawExample();
		CbtChangeDrawExample.Criteria criteria =cbtChangeDrawExample.createCriteria();

		SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
		if(userId!=null){
			criteria.andUserIdEqualTo(userId);
		}
		if(null!=startTime && null!=endTime && !startTime.equals("") && !endTime.equals("")){
			criteria.andCreateDateBetween(formatter.parse(startTime), formatter.parse(endTime));
		}
		if(null!=coinId){
			criteria.andCoinIdEqualTo(coinId);
		}
		
        List<CbtChangeDraw> recharges = this.cbtChangeDrawMapper.selectByExample(cbtChangeDrawExample);
        if(recharges==null||recharges.size()<1) {
        	return null;
        }
        return recharges;
		
	}

	@Override
	public int ListTotal(Integer userId, Integer coinId, String startTime, String endTime, Integer rows)
			throws ParseException {
		// TODO Auto-generated method stub
		CbtChangeDrawExample cbtChangeDrawExample = new CbtChangeDrawExample();
		CbtChangeDrawExample.Criteria criteria =cbtChangeDrawExample.createCriteria();

		SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
		if(userId!=null){
			criteria.andUserIdEqualTo(userId);
		}
		if(null!=startTime && null!=endTime && !startTime.equals("") && !endTime.equals("")){
			criteria.andCreateDateBetween(formatter.parse(startTime), formatter.parse(endTime));
		}
		if(null!=coinId){
			criteria.andCoinIdEqualTo(coinId);
		}
		
		int total=(int) this.cbtChangeDrawMapper.countByExample(cbtChangeDrawExample);
		int totalPages;
		if(total%rows == 0) {
			totalPages=(int) (total/rows);
		}else {
			totalPages=(int) (total/rows)+1;
		}
		System.out.println("totalPages_recharge=>"+totalPages);
		return totalPages;
	}

	@Override
	public double allChange(Integer coinId, String startTime, String endTime) throws ParseException {
		// TODO Auto-generated method stub
		SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
		Date data1 =null;
		Date data2 =null;
		
		if(null!=startTime && null!=endTime && !startTime.equals("") && !endTime.equals("")){
			 data1 = formatter.parse(startTime);
			 data2 = formatter.parse(endTime);
		}
		return cbtChangeDrawMapper.allChange(coinId,data1, data2);
	}

}
